package com.han.http.fetch;

import com.han.http.client.exception.RequestNotExecutedException;
import com.han.http.client.method.BaseHttpRequest;
import com.han.http.client.method.HttpGet;
import com.han.http.client.method.HttpPost;
import com.han.http.client.paramter.FormDataPair;
import com.han.http.client.paramter.RequestHeader;

import java.net.Proxy;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Han
 * @date: 2018/6/29 15:34
 * @dscription: 集成获取指定url的内容
 */
public class Fetcher {

    /**
     * 使用指定参数获取指定url的内容
     *
     * @param urlStr         url
     * @param requestHeaders 请求头
     * @param connectTimeout 连接超时时间
     * @param readTimeout    读取超时时间
     * @param isRedirect     是否重定向
     * @param proxy          代理
     * @return 目标内容
     * @throws Exception 抛出各种异常
     */
    public String getPageInfoByGet(String urlStr, RequestHeader[] requestHeaders, int connectTimeout,
                                   int readTimeout, boolean isRedirect, Proxy proxy) throws Exception {

        Map<String, List<String>> map = getAllInfoByGet(urlStr, requestHeaders, connectTimeout, readTimeout, isRedirect,
                proxy);
        List<String> contents = map.get("page-content");
        StringBuilder stringBuilder = new StringBuilder();
        for (String tempStr : contents) {
            stringBuilder.append(tempStr).append("\n");
        }

        return stringBuilder.toString();
    }

    /**
     * 获取包含页面和响应头的所有内容
     *
     * @param urlStr         地址
     * @param requestHeaders 请求头
     * @param connectTimeout 连接超时
     * @param readTimeout    读取超时
     * @param isRedirect     是否重定向
     * @param proxy          代理
     * @return 所有内容
     * @throws Exception 抛出异常
     */
    public Map<String, List<String>> getAllInfoByGet(String urlStr, RequestHeader[] requestHeaders, int connectTimeout,
                                                     int readTimeout, boolean isRedirect, Proxy proxy) throws Exception {
        Map<String, List<String>> map = new LinkedHashMap<>();

        HttpGet httpGet = BaseHttpRequest.getGetMethod(urlStr);
        httpGet.setConnectTimeout(1000 * connectTimeout);
        httpGet.setReadTimeout(1000 * readTimeout);
        httpGet.setHeaders(requestHeaders);
        httpGet.setProxy(proxy);
        httpGet.setRedirects(isRedirect);
        try {
            httpGet.execute();
            String info = httpGet.getText();
            map.put("page-content", Collections.singletonList(info));
            Map<String, List<String>> tempMap = httpGet.getHeaderFields();
            map.putAll(tempMap);
        } finally {
            try {
                httpGet.disconnect();
            } catch (RequestNotExecutedException e) {
                e.printStackTrace();
            }
        }

        return map;
    }

    /**
     * 通过post获取页面内容
     *
     * @param urlStr         地址
     * @param requestHeaders 请求头
     * @param formDataPairs  form数据
     * @param connectTimeout 连接超时时间
     * @param readTimeout    读取超时时间
     * @param isRedirect     是否重定向
     * @param proxy          代理
     * @return 页面内容
     * @throws Exception 抛出异常
     */
    public String getPageInfoByPost(String urlStr, RequestHeader[] requestHeaders, FormDataPair[] formDataPairs, int
            connectTimeout, int readTimeout, boolean isRedirect, Proxy proxy) throws Exception {

        Map<String, List<String>> map = getAllInfoByPost(urlStr, requestHeaders, formDataPairs, connectTimeout, readTimeout,
                isRedirect, proxy);
        List<String> contents = map.get("page-content");
        StringBuilder stringBuilder = new StringBuilder();
        for (String tempStr : contents) {
            stringBuilder.append(tempStr).append("\n");
        }

        return stringBuilder.toString();
    }

    /**
     * 获取包含页面和响应头的所有内容
     *
     * @param urlStr         地址
     * @param requestHeaders 请求头
     * @param formDataPairs  form数据
     * @param connectTimeout 连接超时时间
     * @param readTimeout    读取超时时间
     * @param isRedirect     是否重定向
     * @param proxy          代理
     * @return 页面和响应头的所有内容
     * @throws Exception 抛出异常
     */
    public Map<String, List<String>> getAllInfoByPost(String urlStr, RequestHeader[] requestHeaders, FormDataPair[] formDataPairs, int
            connectTimeout, int readTimeout, boolean isRedirect, Proxy proxy) throws Exception {
        Map<String, List<String>> map = new LinkedHashMap<>();

        HttpPost httpPost = BaseHttpRequest.getPostMethod(urlStr);
        httpPost.setConnectTimeout(connectTimeout);
        httpPost.setReadTimeout(readTimeout);
        httpPost.setHeaders(requestHeaders);
        httpPost.setFormDataPairs(formDataPairs);
        httpPost.setProxy(proxy);
        httpPost.setRedirects(isRedirect);
        try {
            httpPost.execute();
            String info = httpPost.getText();
            map.put("page-content", Collections.singletonList(info));
            Map<String, List<String>> tempMap = httpPost.getHeaderFields();
            map.putAll(tempMap);
        } finally {
            try {
                httpPost.disconnect();
            } catch (RequestNotExecutedException e) {
                e.printStackTrace();
            }
        }

        return map;
    }
}
